from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware

from shopping_app.views.cart_views import shop
from ch01.views import ch1
from ch02.views import ch2
from ch03.views import ch3
from ch06.views import ch6
from ch07.views import ch7

app = FastAPI()

# CORS
origins = [
    "http://localhost:8000",
    "http://127.0.0.1:8000",
    'http://192.168.0.66:8000/',
]
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 把项目下的static目录作为静态文件的访问目录
app.mount('/static', StaticFiles(directory='static'), name='my_static')

# 加载所有分路由
app.include_router(shop)
app.include_router(ch1)
app.include_router(ch2)
app.include_router(ch3)
app.include_router(ch6)
app.include_router(ch7)

@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}


@app.post('/test', tags=['给接口分组的标签'], summary='测试的接口', description='接口的详细描述', response_description='响应数据的详细描述')
def test():
    print('执行了test函数')
    return {'msg': 'OK!'}


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app)